using System;
using System.Xml.Serialization;

namespace CurrencyConv.Core
{
	[XmlRoot]
	public class Money
	{
		public Money()
		{

		}
		public Money(string currencyCode, string currencyName, decimal ammount)
		{
			CurrencyCode = currencyCode;
			Ammount = ammount;
			CurrencyName = currencyName;
		}

		public Money(Money money, decimal ammount)
		{
			CurrencyCode = money.CurrencyCode;
			CurrencyName = money.CurrencyName;
			Ammount = ammount;
		}

		public string CurrencyCode { get; set; }

		public string CurrencyName { get; set; }

		private decimal _ammount;
		public decimal Ammount
		{
			get { return Math.Round(_ammount, 2); }
			set { _ammount = value; }
		}

		public override string ToString()
		{
			return string.Format(" {2} {0}, - CurrencyName: {1}", CurrencyCode, CurrencyName, Ammount);
		}

		#region Override Equals

		public override bool Equals(object obj)
		{
			if (ReferenceEquals(null, obj)) return false;
			if (ReferenceEquals(this, obj)) return true;
			if (obj.GetType() != typeof(Money)) return false;
			return Equals((Money)obj);
		}

		public bool Equals(Money other)
		{
			if (ReferenceEquals(null, other)) return false;
			if (ReferenceEquals(this, other)) return true;
			return Equals(other.CurrencyCode, CurrencyCode) && Math.Round(other.Ammount, 2) == Math.Round(Ammount, 2);
		}

		public override int GetHashCode()
		{
			unchecked
			{
				int result = (CurrencyCode != null ? CurrencyCode.GetHashCode() : 0);
				result = (result * 397) ^ Ammount.GetHashCode();
				return result;
			}
		}


		#endregion
	}
}